﻿using System.Web.Mvc;
using Matrix.MaKaYunShang.BLL;
using Matrix.MaKaYunShang.Model.Dto;

namespace Matrix.MaKaYunShang.McMagWeb.Controllers
{
    [Authorization]
    public class MessageController : BaseController
    {
        private readonly McMessageRecipientsBLL _bll = new McMessageRecipientsBLL();

        // GET: Message
        public ActionResult List()
        {
            return View();
        }

        [HttpPost]
        public JsonResult Listing(int draw, int start, int length)
        {
            int shopId = CurrentUser.ShopId;
            int order;
            int.TryParse(Request["order[0][column]"], out order);
            string direction = Request["order[0][dir]"];
            var orderByColumns = new[] {"Created", "Title", "Body", "IsRead"};
            string search = Request["search[value]"];
            int pageIndex = (start/length) + 1;

            TableDataViewDto<ViewMcMessageReceivedDto> theList = _bll.GetPagingListForShop(draw, pageIndex, length,
                shopId, SqlSafe(search),
                orderByColumns[order] + " " + direction);

            JsonResult jsonResult = Json(theList);
            jsonResult.MaxJsonLength = int.MaxValue;

            return jsonResult;
        }

        [HttpPost]
        public JsonResult GetLastestMessage()
        {
            int shopId = CurrentUser.ShopId;

            TableDataViewDto<ViewMcMessageReceivedDto> theList = _bll.GetPagingListForShop(0, 1, 11, shopId, "", null);

            return Json(theList);
        }

        /// <summary>
        /// 所有消息数
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetMessageCount()
        {
            int shopId = CurrentUser.ShopId;

            TableDataViewDto<ViewMcMessageReceivedDto> theList = _bll.GetPagingListForShop(0, 1, 1000, shopId, "", null);

            return Json(theList.data.Count);
        }

        [HttpPost]
        [PermissionFilter]
        public JsonResult Delete()
        {
            string ids = Request["doIds"];
            bool result = _bll.DeleteList(ids);

            return Json(result);
        }

        /// <summary>
        /// 所有消息数
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult SetAllRead()
        {
            var result = _bll.UpdateAlllRead();
            return Json(result);
        }
    }
}